package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;

import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CoreIntegral;
import com.xiaoyu.lingdian.entity.CoreBookingRecord;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CoreIntegralService;
import com.xiaoyu.lingdian.service.CoreBookingRecordService;
import com.xiaoyu.lingdian.vo.CoreBookingRecordVO;

@Controller
@RequestMapping(value="/coreBookingRecord")
public class CoreBookingRecordController extends BaseController {

	/**
	* coreBookingRecord表
	*/
	@Autowired
	private CoreBookingRecordService coreBookingRecordService;
	
	/**
	 * 积分攻略表
	 */
	@Autowired
	private CoreIntegralService coreIntegralService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	* 添加预约信息
	*
	* @param crkrdType 类型(1快递2回收)
	* @param crkrdJjArea 姓名
	* @param crkrdJjHouse 手机
	* @param crkrdJjBand 小区名
	* @param crkrdJjName 详细地址：几幢几单元几零几
	* @param crkrdJjValue 时间
	* @param crkrdUser 下单用户UUID
	* @param crkrdRemarks 备注
	* @param uuids
	* @return
	*/
	@RequestMapping(value = "/add/coreBookingRecord", method = RequestMethod.POST)
	public void addCoreBookingRecord(Integer crkrdType, String crkrdJjArea,
			String crkrdJjHouse, String crkrdJjBand, String crkrdJjName,
			String crkrdJjValue, String crkrdUser, String crkrdRemarks,
			String uuids, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin addCoreBookingRecord");

		if (null == crkrdType || 0 == crkrdType) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[类型]不能为空!"), response);
			return;
		}
		
		if (StringUtil.isEmpty(crkrdUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户UUID]不能为空!"), response);
			return;
		}
		CoreBookingRecord coreBookingRecord = new CoreBookingRecord();
		String uuid = "KD"+RandomUtil.generateString(14);
		coreBookingRecord.setCrkrdUuid(uuid);
		coreBookingRecord.setCrkrdType(crkrdType);
		coreBookingRecord.setCrkrdStatus(1);
		coreBookingRecord.setCrkrdJjArea(crkrdJjArea);
		coreBookingRecord.setCrkrdJjHouse(crkrdJjHouse);
		coreBookingRecord.setCrkrdJjBand(crkrdJjBand);
		coreBookingRecord.setCrkrdJjName(crkrdJjName);
		coreBookingRecord.setCrkrdJjValue(crkrdJjValue);
		coreBookingRecord.setCrkrdUser(crkrdUser);
		coreBookingRecord.setCrkrdRemarks(crkrdRemarks);
		coreBookingRecord.setCrkrdCdate(new Date());

		coreBookingRecordService.insertCoreBookingRecord(coreBookingRecord);

		// 添加图片
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(uuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreBookingRecordController]:end addCoreBookingRecord");
	}
	
	/**
	* 修改
	*
	* @param crkrdUuid 标识UUID
	* @param crkrdStatus 状态（2审核通过3审核未通过7已完成）
	* @param crkrdRemarks 备注
	* @return
	*/
	@RequestMapping(value = "/update/coreBookingRecord", method = RequestMethod.POST)
	public void updateCoreBookingRecord(String crkrdUuid, Integer crkrdStatus,
			String crkrdRemarks, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin updateCoreBookingRecord");

		if (StringUtil.isEmpty(crkrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreBookingRecord coreBookingRecord = new CoreBookingRecord();
		coreBookingRecord.setCrkrdUuid(crkrdUuid);
		coreBookingRecord.setCrkrdStatus(crkrdStatus);
		coreBookingRecord.setCrkrdRemarks(crkrdRemarks);
		coreBookingRecord.setCrkrdUdate(new Date());

		coreBookingRecordService.updateCoreBookingRecord(coreBookingRecord);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreBookingRecordController]:end updateCoreBookingRecord");
	}

	/**
	* 删除
	*
	* @param crkrdUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreBookingRecord (String crkrdUuid, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin deleteCoreBookingRecord");

		if (StringUtil.isEmpty(crkrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}

		CoreBookingRecord coreBookingRecord = new CoreBookingRecord();
		coreBookingRecord.setCrkrdUuid(crkrdUuid);

		coreBookingRecordService.deleteCoreBookingRecord(coreBookingRecord);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreBookingRecordController]:end deleteCoreBookingRecord");

	}

	/**
	* 批量删除
	*
	* @param crkrdUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreBookingRecord (String crkrdUuids, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin deleteBatchCoreBookingRecord");

		if (StringUtil.isEmpty(crkrdUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}

		String[] uuids=crkrdUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreBookingRecordService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreBookingRecordController]:end deleteBatchCoreBookingRecord");

	}

	/**
	* 获取单个
	*
	* @param crkrdUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreBookingRecord (String crkrdUuid, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin viewsCoreBookingRecord");

		if (StringUtil.isEmpty(crkrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}

		CoreBookingRecord coreBookingRecord = new CoreBookingRecord();
		coreBookingRecord.setCrkrdUuid(crkrdUuid);

		coreBookingRecord = coreBookingRecordService.getCoreBookingRecord(coreBookingRecord);

		CoreBookingRecordVO coreBookingRecordVO = new CoreBookingRecordVO();
		coreBookingRecordVO.convertPOToVO(coreBookingRecord);
		//获得相应的积分
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		double fee = 0.0;
		try {
			fee = Double.parseDouble(coreBookingRecord.getCrkrdTotal());
		} catch (Exception e) {
		}
		long a = Math.round((points * fee) / 10.0);
		coreBookingRecordVO.setKhdScore(Integer.parseInt(a+""));
		//查询附件UUID
		List<CoreAttachment> list = coreAttachmentService.findCoreAttachmentByCnd(crkrdUuid);
		if (list != null && list.size() > 0) {
			coreBookingRecordVO.setHeadUuid(list.get(0).getCratmUuid());
		}
				
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreBookingRecordVO), response);
		logger.info("[CoreBookingRecordController]:end viewsCoreBookingRecord");

	}

	/**
	* 获取列表<Page>	
	* 
	* @param pageNum 页码
	* @param pageSize 页数
	* @param crkrdType 类型(1快递2回收)
	* @param crkrdUser 下单用户UUID
	* @param crkrdJjArea 预约姓名
	* 
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreBookingRecordPage (Integer pageNum, Integer pageSize, Integer crkrdType, String crkrdUser, String crkrdJjArea, HttpServletResponse response) {
		logger.info("[CoreBookingRecordController]:begin findCoreBookingRecordPage");
		
		if (null == crkrdType || 0 == crkrdType) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[类型]不能为空!"), response);
			return;
		}
		
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreBookingRecord> page = coreBookingRecordService.findCoreBookingRecordPage(pageNum, pageSize, crkrdType, crkrdUser, crkrdJjArea);
		Page<CoreBookingRecordVO> pageVO = new Page<CoreBookingRecordVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreBookingRecordVO> vos = new ArrayList<CoreBookingRecordVO>();
		CoreBookingRecordVO vo;
		//获得相应的积分
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		for (CoreBookingRecord coreBookingRecord : page.getResult()) {
			vo = new CoreBookingRecordVO();

			vo.setCrkrdUuid(coreBookingRecord.getCrkrdUuid());
			vo.setCrkrdType(coreBookingRecord.getCrkrdType());
			vo.setCrkrdStatus(coreBookingRecord.getCrkrdStatus());
			vo.setCrkrdTotal(coreBookingRecord.getCrkrdTotal());
			vo.setCrkrdSyScore(coreBookingRecord.getCrkrdSyScore());
			vo.setCrkrdDkFee(coreBookingRecord.getCrkrdDkFee());
			vo.setCrkrdSjFee(coreBookingRecord.getCrkrdSjFee());
			vo.setCrkrdJjArea(coreBookingRecord.getCrkrdJjArea());
			vo.setCrkrdJjHouse(coreBookingRecord.getCrkrdJjHouse());
			vo.setCrkrdJjBand(coreBookingRecord.getCrkrdJjBand());
			vo.setCrkrdJjName(coreBookingRecord.getCrkrdJjName());
			vo.setCrkrdJjValue(coreBookingRecord.getCrkrdJjValue());
			vo.setCrkrdUser(coreBookingRecord.getCrkrdUser());
			vo.setCrkrdUserName(coreBookingRecord.getCrkrdUserName());
			vo.setCrkrdRemarks(coreBookingRecord.getCrkrdRemarks());
			vo.setCrkrdCdate(DateUtil.formatTimesTampDate(coreBookingRecord.getCrkrdCdate()));
			vo.setCrkrdUdate(DateUtil.formatTimesTampDate(coreBookingRecord.getCrkrdUdate()));
			//计算砍价获取的积分
			double fee = 0.0;
			try {
				fee = Double.parseDouble(coreBookingRecord.getCrkrdTotal());
			} catch (Exception e) {
			}
			long a = Math.round((points * fee) / 10.0);
			vo.setKhdScore(Integer.parseInt(a+""));
			
			vos.add(vo);
		}
		pageVO.setResult(vos);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreBookingRecordController]:end findCoreBookingRecordPage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}
